package com.caifeng.weatheractivity;

import android.text.InputType;
import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class WeatherService {
    public static List<WeatherInfo> getInfoFromXml(InputStream is) throws Exception {
        //得到pull解析器
        XmlPullParser xmlPullParser = Xml.newPullParser();
        xmlPullParser.setInput(is, "utf-8");
        List<WeatherInfo> weatherInfos = null;
        WeatherInfo weatherInfo = null;
        //得到当前事件
        int type = xmlPullParser.getEventType();
        while (type != xmlPullParser.END_DOCUMENT) {
            switch (type) {
                case XmlPullParser.START_TAG:
                    if ("infos".equals(xmlPullParser.getName())) {
                        weatherInfos = new ArrayList<WeatherInfo>();

                    } else if ("city".equals(xmlPullParser.getName())) {
                        weatherInfo = new WeatherInfo();
                        String isStr = xmlPullParser.getAttributeValue(0);
                        weatherInfo.setId(isStr);
                    } else if ("temp".equals(xmlPullParser.getName())) {
                        String temp = xmlPullParser.nextText();
                        weatherInfo.setTemp(temp);
                    } else if ("weather".equals(xmlPullParser.getName())) {
                        String weather = xmlPullParser.nextText();
                        weatherInfo.setWeather(weather);
                    } else if ("wind".equals(xmlPullParser.getName())) {
                        String wind = xmlPullParser.nextText();
                        weatherInfo.setWind(wind);
                    } else if ("name".equals(xmlPullParser.getName())) {
                        String name = xmlPullParser.nextText();
                        weatherInfo.setCity(name);
                    } else if ("pm".equals(xmlPullParser.getName())) {
                        String pm = xmlPullParser.nextText();
                        weatherInfo.setPm(pm);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("city".equals(xmlPullParser.getName())) {
                        weatherInfos.add(weatherInfo);
                        weatherInfo = null;
                    }
                    break;
            }
            type = xmlPullParser.next();
        }
        return weatherInfos;
    }
}
